/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.domain;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import nominalitzacions.persistance.SettingsDataManagerController;

/**
 *
 * @author genis
 */
public class SettingsDomainController {

    private Settings settings;
    private Settings defaults;
    private Settings merged;
    private boolean [] checks;

    private SettingsDataManagerController SDMC;

    public SettingsDomainController() throws FileNotFoundException, IOException {
        SDMC = new SettingsDataManagerController();
        checks = SDMC.loadChecks();
        settings = SDMC.loadSettings(false);
        defaults = SDMC.loadSettings(true);
        merged = SDMC.loadSettings(true);
        upDateMerged();
    }

    public void setK(int k) {
        settings.setK(k);
        upDateMerged();
    }

    public int getK() {
        return settings.getK();
    }

    public void setN(int n) {
        settings.setN(n);
        upDateMerged();
    }

    public int getN() {
        return settings.getN();
    }

    public void setBackground(Color bc) {
        settings.setBackground(bc);
        upDateMerged();
    }

    public Color getBackground() {
        return settings.getBackground();
    }

    public void setHighlight(Color hc) {
        settings.setHighlight(hc);
        upDateMerged();
    }

    public Color getHighlight() {
        return settings.getHighlight();
    }

    public void setChecks(boolean [] c) {
        checks = c;
        upDateMerged();
    }
    
    public boolean [] getChecks() {
        return checks;
    }

    public void saveSettings() throws IOException {
        SDMC.saveSettings(settings);
        SDMC.saveChecks(checks);
    }

    public Settings getSettings() {
        return merged;
    }

    private void upDateMerged() {
        
        if (checks[0]) {
            merged.setK(defaults.getK());
            merged.setN(defaults.getN());
        } else {
            merged.setK(settings.getK());
            merged.setN(settings.getN());
        }

        if (checks[1]) {
            merged.setBackground(defaults.getBackground());
        } else {
            merged.setBackground(settings.getBackground());
        }

        if (checks[2]) {
            merged.setHighlight(defaults.getHighlight());
        } else {
            merged.setHighlight(settings.getHighlight());
        }
    }
}
